<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>发改局</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet"
        href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">

  <!-- IonIcons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">

  <link rel="stylesheet" href="../../plugins/fontawesome-free/css/all.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/adminlte.css">
</head>


<body class="hold-transition sidebar-mini">
<div class="wrapper">

  <!-- 主内容容器 -->
  <div class="content-wrapper">
    <!-- 容器头标题 -->
    <div class="content-header">
      <div class="container-fluid">

        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0">计量单位修改</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">首页</a></li>
              <li class="breadcrumb-item active">计量单位修改</li>
            </ol>
          </div>
        </div>

      </div>

    </div>


    <!-- 主题内容正文 -->
    <div class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-6">

            <div class="card card-primary">
              <!-- 卡片头部 -->
              <div class="card-header">
                <h3 class="card-title">计量单位修改</h3>
              </div>
              <!-- 表单开始 -->
              <form>
                <div class="card-body">

                  <input type="hidden" id="unit_id" name="id" th:value="${data.id}" />
                  <div class="form-group">
                    <div class="form-group">
                      <label>计量单位选择</label>
                      <select class="form-control" id="unit_name" name="unitName" >
                        <option value="1" >年</option>
                        <option value="2">重量</option>
                      </select>
                    </div>
                  </div>

                  <div class="form-group">
                    <label for="unitvalue">例如: 2019/2020/2021/2022/吨/千克/克</label>
                    <input type="text" class="form-control" id="unitvalue" name="unitValue" th:value="${data.getUnitValue()}" placeholder="请输入">
                  </div>

                </div>

                <input type="hidden" id="deleted_id" value="0" name="deleted" />
                <!-- 添加按钮 -->
                <div class="card-footer">
                  <button type="button" class="btn btn-primary" id="save_btn">保存</button>
                </div>
              </form>

            </div>

          </div>

        </div>

      </div>

    </div>

  </div>


  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
  </aside>

  <!-- 版权信息 -->
  <footer class="main-footer">
    <strong>Copyright &copy; 2021 <a href="http://www.chengzhouyun.com/" target="_blank">ChengZhouYun</a>.</strong> All
    rights reserved.
    <div class="float-right d-none d-sm-inline-block">
      <b>Version</b> 1.0
    </div>
  </footer>

</div>

<script src="../../plugins/jquery/jquery.js"></script>
<!-- Bootstrap -->
<script src="../../plugins/bootstrap/js/bootstrap.bundle.js"></script>
<!-- AdminLTE -->
<script src="../../dist/js/adminlte.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../../dist/js/demo.js"></script>
</body>

<script th:inline="javascript">

  $(function(){
    const unitname = /*[[${data.unitName}]]*/{};         //{}是默认值,/*[[${user}]]*/ 是传进去的值

    var select = document.getElementById('unit_name');

    if (unitname == '年'){
      select.value = 1;
    }else {
      select.value = 2;
    }

    $("#save_btn").on("click",function(event){
      var formData = new FormData();
      formData.id = $("#unit_id").val();
      formData.unitName = $("#unit_name").find("option:selected").text();
      formData.unitValue = $("#unitvalue").val();
      formData.deleted = $("#deleted_id").val();

      $.ajax({
        url: "/uit/unit",
        type: "put",
        data: JSON.stringify(formData),
        contentType: "application/json",
        async: false,
        dataType: "json",
        success: function (res) {

          if (res.statusCode == 200) {
            alert(res.message);
            // 重载页面
            location.href = "/page/Unit_Info"
          } else {
            alert(res.errorMsg);
          }
        }
      })
    })

  })


</script>
</html>
